<?php

namespace App\Http\Controllers\Admin;

use App\Traits\ApiResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator;
use Exception;

class UploadController extends Controller
{
    use ApiResponse;

    public function file(Request $request)
    {
        $validator = Validator::make([
            'file' => $request->file,
            'extension' => strtolower($request->file->getClientOriginalExtension())
        ], [
            'file' => 'required',
            'extension' => 'required|in:pdf,doc,docx,xlsx,xls,zip,jpeg,jpg,png,txt',
        ], [
            'file.*' => '请上传文件',
            'extension.*' => '文件类型不允许',
        ]);
        if ($validator->fails()) {
            return response()->json(['status' => 0, 'msg' => $validator->errors()->first()]);
        }
        if (!$request->hasFile('file')) {
            return response()->json(['status' => 0, 'msg' => '请上传文件']);
        }

        $fileName = uniqid(true) . '.' . $request->file->getClientOriginalExtension();
//        $baseUrl = $request->file('file')->storePubliclyAs('upload/' . date('Ymd'), $fileName, ['disk' => 'public']);
        $baseUrl = $request->file('file')->storeAs(date('Ymd'), $fileName, 'pic');
        $request->file('file')->isValid();
        return response()->json(['status' => 0, 'data' => ['url' => '/pic/' . $baseUrl, 'filename' => $request->file->getClientOriginalName()]]);
    }

}
